﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Threading.Tasks;
using Elasticsearch.Net;
using ESoft.Extensions;
using Nest;

namespace ESTest {
    class Program {
        static void Main(string[] args) {
            var list = new List<Guid>();
            for (int i = 0; i < 100; i++) {
                Guid g = GenerateGuid();

                Console.WriteLine(g);
                list.Add(g);
            }

            Console.WriteLine();
            Console.WriteLine();
            list.Sort();
            foreach (var item in list) {
                Console.WriteLine(item);
            }
         
        }

        //public static Guid GenerateGuid() {
        //    byte[] guidArray = Guid.NewGuid().ToByteArray();

        //    var baseDate = new DateTime(1900, 1, 1);
        //    DateTime now = DateTime.Now;
        //    var days = new TimeSpan(now.Ticks - baseDate.Ticks);
        //    TimeSpan msecs = now.TimeOfDay;

        //    byte[] daysArray = BitConverter.GetBytes(days.Days);
        //    byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));

        //    Array.Reverse(daysArray);
        //    Array.Reverse(msecsArray);

        //    Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
        //    Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);

        //    return new Guid(guidArray);
        //}

        public static Guid GenerateGuid() {
            byte[] guidArray = Guid.NewGuid().ToByteArray();

            var baseDate = new DateTime(1900, 1, 1);
            DateTime now = DateTime.Now;
            var days = new TimeSpan(now.Ticks - baseDate.Ticks);
            TimeSpan msecs = now.TimeOfDay;

            byte[] daysArray = BitConverter.GetBytes(days.Days);
            byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));

            Array.Reverse(daysArray);
            Array.Reverse(msecsArray);

            Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
            Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);

            return new Guid(guidArray);
        }
    }
    //https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html
    //public abstract class Document
    //{
    //    public JoinField Join { get; set; }
    //}

    //public class Company : Document
    //{
    //    public string Name { get; set; }
    //    public List<Employee> Employees { get; set; }
    //}

    //public class Employee : Document
    //{
    //    public string LastName { get; set; }
    //    public int Salary { get; set; }
    //    public DateTime Birthday { get; set; }
    //    public bool IsManager { get; set; }
    //    public List<Employee> Employees { get; set; }
    //    public TimeSpan Hours { get; set; }
    //}

    [ElasticsearchType(RelationName = "employee")]
    public class Employee {
        [Text(Name = "first_name")]
        public string FirstName { get; set; }

        [Text(Name = "last_name")]
        public string LastName { get; set; }

        [Number(DocValues = false, IgnoreMalformed = true, Coerce = true)]
        public int Salary { get; set; }

        [Date(Format = "MMddyyyy")]
        public DateTime Birthday { get; set; }

        [Boolean(NullValue = false, Store = true)]
        public bool IsManager { get; set; }

        [Nested]
        [PropertyName("empl")]
        public List<Employee> Employees { get; set; }

        [Text(Name = "office_hours")]
        public TimeSpan? OfficeHours { get; set; }

        [Object]
        public List<Skill> Skills { get; set; }
    }

    public class Skill {
        [Text]
        public string Name { get; set; }

        [Number(NumberType.Byte, Name = "level")]
        public int Proficiency { get; set; }
    }
}
